//
//  CycleScrollView.m
//  Charles
//
//  Created by Charles on 2017/2/7.
//  Copyright © 2017年 charles. All rights reserved.
//

#import "CycleScrollView.h"

#define visibleRows 7 // 视野中的rows

@interface CycleScrollView ()
{
    /**
     *  view的宽高，行高
     */
    CGFloat _height;
    CGFloat _width;
    CGFloat _rowHeight;
    
    NSInteger _rows;
    NSMutableArray *_views;
    
    /**
     *  颜色，字体
     */
    NSArray *_colorsAndfonts;
}

@end

@implementation CycleScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self configData];
        
        _height = self.bounds.size.height;
        _rowHeight = _height;
        
        _width = self.bounds.size.width/visibleRows;
        
        /**
         *  选中色彩
         */
        UIView *selectView = [[UIView alloc] initWithFrame:CGRectMake((self.frame.size.width - _width)/2,0, _width, _rowHeight)];
        [selectView setBackgroundColor:[UIColor colorWithRed:227.0 / 255.0 green:255 / 255.0  blue:226 / 255.0  alpha:1.0]];
        [self addSubview:selectView];
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(_width * (visibleRows + 2) ,_rowHeight);
        
        _scrollView.delegate = self;
        _scrollView.backgroundColor = [UIColor clearColor];
        [self addSubview:_scrollView];
    }
    return self;
}

/**
 *  初始化颜色，字体信息
 */
- (void)configData
{
    _colorsAndfonts = @[
                        @{@"color":[UIColor colorWithRed:186.0 / 255.0 green:186.0 / 255.0 blue:186.0 / 255.0 alpha:1.0], @"font":[UIFont systemFontOfSize:14]},
                        @{@"color":[UIColor colorWithRed:186.0 / 255.0 green:186.0 / 255.0 blue:186.0 / 255.0 alpha:1.0], @"font":[UIFont systemFontOfSize:14]},
                        @{@"color":[UIColor colorWithRed:113.0 / 255.0 green:113.0 / 255.0 blue:113.0 / 255.0 alpha:1.0], @"font":[UIFont systemFontOfSize:16]},
                        
                        @{@"color":[UIColor greenColor], @"font":[UIFont systemFontOfSize:18]},
                        
                        @{@"color":[UIColor colorWithRed:113.0 / 255.0 green:113.0 / 255.0 blue:113.0 / 255.0 alpha:1.0], @"font":[UIFont systemFontOfSize:16]},
                        @{@"color":[UIColor colorWithRed:186.0 / 255.0 green:186.0 / 255.0 blue:186.0 / 255.0 alpha:1.0], @"font":[UIFont systemFontOfSize:14]},
                        @{@"color":[UIColor colorWithRed:186.0 / 255.0 green:186.0 / 255.0 blue:186.0 / 255.0 alpha:1.0], @"font":[UIFont systemFontOfSize:14]},
                        @{@"color":[UIColor colorWithRed:186.0 / 255.0 green:186.0 / 255.0 blue:186.0 / 255.0 alpha:1.0], @"font":[UIFont systemFontOfSize:14]}
                        
                        ];
}

- (void)setDataSource:(id<CycleScrollViewDataSource>)dataSource
{
    _dataSource = dataSource;
    [self reloadData];
}

- (void)reloadData
{
    _rows = [_dataSource numberOfRowsInCycleScrollView:self];
    
    if (_rows <= 0) {
        return;
    }
    [self loadView];
}

- (void)loadView
{
    // 移除已添加的row
    NSArray *subViews = [_scrollView subviews];
    if (subViews && subViews.count != 0) {
        [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    // 显示新的row
    [self createRows];
    [_scrollView setContentOffset:CGPointMake(_width, 0)];
}

/**
 *  创建rows内容
 */
- (void)createRows
{
    if (_views) {
        _views = [NSMutableArray array];
    } else {
        [_views removeAllObjects];
    }
    
    NSInteger colorIndex = 1;
    for (NSInteger i = _index - 3; i <= _index + 3; i ++) {
        NSInteger index =  [self getValidateIndex:i];
        id data;
        
        if (colorIndex < _colorsAndfonts.count) {
            data = [_colorsAndfonts objectAtIndex:colorIndex];
        }
        
        UILabel *label = (UILabel*)[_dataSource cycleScrollView:self viewForRowAtIndex:index];
        label.font = [data objectForKey:@"font"];
        label.textColor = [data objectForKey:@"color"];
        label.backgroundColor = [UIColor clearColor];
        [_views addObject:label];
        
        label.frame = CGRectMake(_width * colorIndex + 1,0, _width, _rowHeight);
        [_scrollView addSubview:label];
        colorIndex ++;
    }
}

/**
 *  滑动后 ，更新界面颜色，字体
 *
 *  @param index 索引
 */
- (void)setAfterDisplayWithIndex:(NSInteger)index
{
    NSInteger colorIndex = 1;
    for (NSInteger i = index - 2; i <= index + 2; i ++) {
        UILabel *label = nil;
        if (i < _scrollView.subviews.count) {
            label = (UILabel *)[[_scrollView subviews] objectAtIndex:i];
        }
        
        id data;
        
        if (colorIndex < _colorsAndfonts.count) {
            data = [_colorsAndfonts objectAtIndex:colorIndex];
        }
        
        
        label.font = [data objectForKey:@"font"];
        label.textColor = [data objectForKey:@"color"];
        
        colorIndex ++;
    }
}

#pragma mark UIScrollView delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat x = scrollView.contentOffset.x;
    
    /**
     *  当滑倒第二行或者第一行结束时，需加载新的
     */
    if ( x >= _width * 2) {
        _index = [self getValidateIndex:_index + 1];
        
        [self loadView];
    } else if (x <= 0) {
        _index = [self getValidateIndex:_index - 1];
        
        [self loadView];
    }
    
    /**
     *  更新滑动后信息
     */
    [self setAfterDisplayWithIndex:3];
    
    /**
     *  响应结果事件
     */
    if ([self.delegate respondsToSelector:@selector(cycleScrollView:didSelectedAtIndex:)]) {
        [self.delegate cycleScrollView:self didSelectedAtIndex:_index];
    }
}

/**
 *  返回对应的数据下标
 *
 *  @param index   NSInteger
 *
 *  @return NSInteger
 */
- (NSInteger)getValidateIndex:(NSInteger)index;
{
    return index >= 0 ? (index % _rows) : (index + _rows);
}

/**
 *  滑动结束后，设置UIScrollView在第二行
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [scrollView setContentOffset:CGPointMake(_width, 0) animated:YES];
}

/**
 *  滑动结束后，设置UIScrollView在第二行
 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [scrollView setContentOffset:CGPointMake(_width, 0) animated:YES];
}

@end
